package com.zq.it.service.impl.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.zq.it.pojo.Chapter;
import com.zq.it.pojo.Course;
import com.zq.it.service.admin.ChapterService;
import com.zq.it.service.common.BaseServiceImpl;

public class ChapterServiceImpl extends BaseServiceImpl implements ChapterService {

	//private List<Chapter> CHAPTERLIST; //存储查询数据
	@Override
	public List<Chapter> getChapterListByParam(Chapter chapter){		
		// TODO Auto-generated method stub
		return getChapterDao().findChapterEtyList(chapter.getChapterId(),chapter.getChapterName());
	}

	@Override
	public void removeChapter(Integer id) {
		getChapterDao().deleteChapterEtyByID(id);
		// TODO Auto-generated method stub	
	}

	@Override
	public void renewalChapter(Chapter chapter){
        getChapterDao().updateChapterEty(chapter);
	}

	@Override
	public List getChapter(Integer id) {
		return getChapterDao().findChapterEtyById(id);
	}

	@Override
	public void addChapter(Chapter chapter){	
		getChapterDao().saveChapterEty(chapter);
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public List getChapterListByType(String type,String parameter) {
		List list=null;
		if(type.equals("0"))
			list=getChapterDao().findChapterEtyById(Integer.parseInt(parameter));
		if(type.equals("1"))
			list=getChapterDao().findChapterEtyByName(parameter);
		return list;
	}
	@Override
	public List getChapterByExmp(Chapter chapter){
		// TODO Auto-generated method stub
		return getChapterDao().findChapterEtyByExmp(chapter);
	}

	@Override
	public void removeChapter(Chapter chapter) {
		// TODO Auto-generated method stub
		getChapterDao().deleteChapterEtyByObj(chapter);
	}

	@Override
	public List<Chapter> importChapterList(File acmyFile){
		  List<Chapter> acmyList=getAcmyListFromStr(importxlsxFile(acmyFile));
		  List<Chapter> existAcmyList=new ArrayList<Chapter>();
		  
		 for(int i=0;i<acmyList.size();i++){		
			 Chapter acmy=acmyList.get(i);
			 if(!getChapterDao().findChapterEtyById(acmy.getChapterId()).isEmpty())
				 existAcmyList.add(acmy);
		 }
		     acmyList.removeAll(existAcmyList);
		   if(!acmyList.isEmpty())
		    	getChapterDao().saveChapterEtyList(acmyList);
			return existAcmyList;
		}	
		 private  List<Chapter> getAcmyListFromStr(String str){		 
			List<Chapter> list=new ArrayList<Chapter>();
			String [] firstArray=str.split(";");
			for(int i=0;i<firstArray.length;i++){
				String [] secondArray=firstArray[i].split(":");
				Chapter chapter=new Chapter();				
				chapter.setChapterName(secondArray[0]);
				chapter.setCourse(new Course(Integer.parseInt(secondArray[1])));
				list.add(chapter);
			}
			return list;
		}		 
	//数据的导出
	@Override
		public ByteArrayInputStream exportChapterList(String[] headTitle,List<Chapter> chapter) {
			// TODO Auto-generated method stub
			return new ByteArrayInputStream(exportToExcel(chapter,headTitle,com.zq.it.tool.HeadTitle.EN_CHAPTERHEAD.getArray(),"com.zq.it.pojo.Chapter").toByteArray());
		}
	@Override
	public List getChapterListBySQL(Chapter chapter){		
		String name=chapter.getChapterName();
     	Course c=chapter.getCourse();
	//	StringBuilder builder=new StringBuilder("select chr.*,c.* from chapter as chr,course c where chr.fkcourseId=c.courseId ");
     	StringBuilder builder=new StringBuilder("0=0\n");
     	if(name!=null&&!"".equals(name))
			builder.append("and chapterName like '%"+chapter.getChapterName()+"%'");
		if(c.getCourseId()>0)
			builder.append(" and fkcourseId="+c.getCourseId());
	 //return getChapterDao().findChapterListBySQL(builder.toString(),chapter.getClass());
		return getChapterDao().findChapterListEtyByFetchMode("course",builder.toString());
	}
	@Override
	public void removeAllChapter(int[] ids){
	   getChapterDao().deleteAllEty(ids);
		// TODO Auto-generated method stub		
	}
	

	@Override
	public List getCourseNameAndId() {
		// TODO Auto-generated method stub
		return getCourseDao().findCourseNameAndId();
	}

	@Override
	public List getAllChapterList() {
		// TODO Auto-generated method stub
		return  getChapterDao().findChapterAllEty();
	}
		
}
